﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Globalization;

namespace ExpressionEvaluator.BuilderTreeNode
{
	/// <summary>
	/// Base builder tree node.
	/// </summary>
	internal abstract class BuilderTreeNodeBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="BuilderTreeNode"/> class.
		/// </summary>
		public BuilderTreeNodeBase()
		{
			Nodes = new List<BuilderTreeNodeBase>();
			Idents = new List<CodeBlock>();
		}

		/// <summary>
		/// Gets or sets the node idents.
		/// </summary>
		/// <value>The node idents.</value>
		public List<CodeBlock> Idents
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the parent.
		/// </summary>
		/// <value>The parent.</value>
		public BuilderTreeNodeBase Parent
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the nodes.
		/// </summary>
		/// <value>The nodes.</value>
		public List<BuilderTreeNodeBase> Nodes
		{
			get;
			internal set;
		}

		/// <summary>
		/// Gets the expression.
		/// </summary>
		/// <param name="tree">The tree.</param>
		/// <returns>Expression value of this node.</returns>
		public abstract Expression GetExpression(BuilderTree tree);

		/// <summary>
		/// Moves the last to new node.
		/// </summary>
		/// <param name="newNode">The new node.</param>
		public void MoveLastToNewNode(ExpressionNode newNode)
		{
			// move last node to new node (while reordering tree for operators)
			BuilderTreeNodeBase lastNode = Nodes[Nodes.Count - 1];
			newNode.Nodes.Add(lastNode);
			Nodes.Remove(lastNode);
		}
	}
}
